﻿using System.Collections.Generic;
using System.Linq;
using Abot.Crawler;
using Abot.Poco;
using Crawler.Abot.Configurations;
using Crawler.Domain;

namespace Crawler.Abot.Crawlers
{
	internal class CrawlerProvider : ICrawlerProvider
	{
		private readonly IEnumerable<IConfigurator> _configurators;

		public CrawlerProvider(IEnumerable<IConfigurator> configurators)
		{
			_configurators = configurators;
		}

		public IWebCrawler Get(TorrentSource source)
		{
			var configurator = GetConfigurator(source);
			var cfg = configurator.BuildConfiguration();

			return new PoliteWebCrawler(cfg, 
				configurator.DecisionMaker,
				configurator.ThreadManager,
				configurator.Scheduler,
				configurator.HttpRequester,
				configurator.HyperLinkParser,
				configurator.MemoryManager,
				configurator.DomainRateLimiter,
				configurator.RobotsDotTextFinder);
		}

		private IConfigurator GetConfigurator(TorrentSource source)
		{
			return _configurators.Single(x => x.ForType == source);
		}
	}
}